//
//  NSString+Regex.m
//  MoYou
//
//  Created by she1323 on 16/10/10.
//  Copyright © 2016年 she1323. All rights reserved.
//

#import "NSString+Regex.h"

@implementation NSString (Regex)

+(NSString *)judegeIsNullAndTransform:(NSString *)transstr{
    return transstr == nil ? @"":transstr;
}

#pragma 正则匹配手机号
+ (BOOL)checkTelNumber:(NSString *) telNumber
{
    NSString *pattern = @"^1+[3578]+\\d{9}";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern];
    BOOL isMatch = [pred evaluateWithObject:telNumber];
    return isMatch;
}


#pragma 正则匹配用户密码6-18位数字和字母组合
+ (BOOL)checkPassword:(NSString *) password
{
    NSString *pattern = @"^(?![0-9]+$)(?![a-zA-Z]+$)[a-zA-Z0-9]{6,18}";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern];
    BOOL isMatch = [pred evaluateWithObject:password];
    return isMatch;
    
}
#pragma 正则匹配用户姓名,20位的中文或英文
+ (BOOL)checkUserName : (NSString *) userName
{
    NSString *pattern = @"^[a-zA-Z\u4E00-\u9FA5]{1,20}";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern];
    BOOL isMatch = [pred evaluateWithObject:userName];
    return isMatch;
    
}


#pragma 正则匹配用户身份证号15或18位
+ (BOOL)checkUserIdCard: (NSString *) idCard
{
    NSString *pattern = @"(^[0-9]{15}$)|([0-9]{17}([0-9]|X)$)";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern];
    BOOL isMatch = [pred evaluateWithObject:idCard];
    return isMatch;
}
#pragma 正则匹员工号,12位的数字
+ (BOOL)checkEmployeeNumber : (NSString *) number
{
    NSString *pattern = @"^[0-9]{12}";
    
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern];
    BOOL isMatch = [pred evaluateWithObject:number];
    return isMatch;
    
}

#pragma 正则匹配URL
+ (BOOL)checkURL : (NSString *) url
{
    NSString *pattern = @"^[0-9A-Za-z]{1,50}";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern];
    BOOL isMatch = [pred evaluateWithObject:url];
    return isMatch;
}
#pragma 设置文字竖直排列
+(NSString *)VerticalStringWithString:(NSString *)string{
    NSMutableString * str = [[NSMutableString alloc] initWithString:string];
    NSInteger count = str.length;
    for (int i = 1; i < count; i ++) {
        [str insertString:@"\n" atIndex:i*2 - 1];
    }
    return str;
}


#pragma mark---根据指定文本,字体和最大高度计算尺寸
+ (CGSize)sizeWidthWidth:(NSString *)text font:(UIFont *)font maxHeight:(CGFloat)height{
    
    NSMutableDictionary *attrDict = [NSMutableDictionary dictionary];
    attrDict[NSFontAttributeName] = font;
    CGSize size = [text boundingRectWithSize:CGSizeMake(MAXFLOAT, height) options:NSStringDrawingUsesLineFragmentOrigin attributes:attrDict context:nil].size;
    return size;
}


-(BOOL)isValidPhoneNumber{
    NSString *regex = @"1[3|5|7|8|][0-9]{9}";
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    return [predicate evaluateWithObject:self];
}

///*  判断用户输入的密码是否符合规范，符合规范的密码要求：
//1. 长度大于8位
//2. 密码中必须同时包含数字和字母
//*/
- (BOOL)judgePassWordLegal{
    BOOL result = false;
    if ([self length] >= 8 && [self length] <= 20){
        // 判断长度大于8位后再接着判断是否同时包含数字和字符
        NSString * regex = @"^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,20}$";
        NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
        result = [pred evaluateWithObject:self];
    }
    return result;
}

+ (NSString *)isOrNoPasswordStyle:(NSString *)passWordName
{
    
    NSString * message;
    
    if (passWordName.length<8) {
        
        message=@" 密码不能少于8位，请您重新输入";
        
    }
    
    else if (passWordName.length>20)
        
    {
        
        message=@"密码最大长度为20位，请您重新输入";
        
    }
    
    else if ([self JudgeTheillegalCharacter:passWordName])
        
    {
        
        message=@"密码不能包含特殊字符，请您重新输入";
        
    }
    
    else if (![self judgePassWordLegal:passWordName])
        
    {
        message=@"密码必须同时包含字母和数字";
    }
    
    return message;
    
}

+(BOOL)JudgeTheillegalCharacter:(NSString *)content{
    
    //提示标签不能输入特殊字符
    
    NSString *str =@"^[A-Za-z0-9\\u4e00-\u9fa5]+$";
    
    NSPredicate* emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", str];
    
    if (![emailTest evaluateWithObject:content]) {
        
        return YES;
    }
    
    return NO;
    
}

+(BOOL)judgePassWordLegal:(NSString *)pass{
    
    BOOL result ;
    
    // 判断长度大于8位后再接着判断是否同时包含数字和大小写字母
    
    NSString * regex = @"^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,20}$";
    
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    
    result = [pred evaluateWithObject:pass];
    
    return result;
    
}

/**
 给一个时间，给一个数，正数是以后n个月，负数是前n个月

 @param date 日期
 @param month 月份
 @return 时间
 */
+(NSDate *)getPriousorLaterDateFromDate:(NSDate *)date withMonth:(NSInteger)month
 {

   NSDateComponents *comps = [[NSDateComponents alloc] init];

    [comps setMonth:month];

     NSCalendar *calender = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDate *mDate = [calender dateByAddingComponents:comps toDate:date options:0];

    return mDate;

}
+(NSDate *)getDateAccordingTimeStr:(NSString *)timeStr{
         NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
         [dateFormatter setDateFormat:@"yyyy-MM-dd"];
         NSDate *date = [dateFormatter dateFromString:timeStr];
         return date;
}
+(NSString *)getTimeStrAccoringDate:(NSDate *)date{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    //设置格式：zzz表示时区
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    //NSDate转NSString
    NSString *currentDateString = [dateFormatter stringFromDate:date];
    return currentDateString;
}
+(NSString *)getCurrentTime{
    //获取当前时间日期
    NSDate *date=[NSDate date];
    NSDateFormatter *format1=[[NSDateFormatter alloc] init];
    [format1 setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *dateStr;
    dateStr=[format1 stringFromDate:date];
    NSLog(@"%@",dateStr);
    return dateStr;
}
+ (BOOL) isBlankString:(NSString *)string {
    
    if (string == nil || string ==NULL) {
        
        return YES;
        
    }
    
    if ([string isKindOfClass:[NSNull class]]) {
        
        return YES;
        
    }
    
    if ([[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length]==0) {//特殊字符判断
        
        return YES;
        
    }
    
    return NO;
    
}
+ (NSString *)stringWithTime:(CGFloat)time;
{
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:time];
    NSDateFormatter *dateFmt = [[NSDateFormatter alloc] init];
    
    if (time >= 3600) {
        [dateFmt setDateFormat:@"HH:mm:ss"];
    } else {
        [dateFmt setDateFormat:@"mm:ss"];
    }
    return [dateFmt stringFromDate:date];
}
+(NSInteger)getNowTimestamp{
    
    
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    
    [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"]; // ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
    
    //设置时区,这个对于时间的处理有时很重要
    
    NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];
    
    [formatter setTimeZone:timeZone];
    
    NSDate *datenow = [NSDate date];//现在时间
    
    
    
    NSLog(@"设备当前的时间:%@",[formatter stringFromDate:datenow]);
    
    //时间转时间戳的方法:
    
    
    
    NSInteger timeSp = [[NSNumber numberWithDouble:[datenow timeIntervalSince1970]] integerValue];
    
    
    
    NSLog(@"设备当前的时间戳:%ld",(long)timeSp); //时间戳的值
    
    
    
    return timeSp;
    
}
+(NSInteger)timeSwitchTimestamp:(NSString *)formatTime andFormatter:(NSString *)format{
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    
    [formatter setDateFormat:format]; //(@"YYYY-MM-dd hh:mm:ss") ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
    
    NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];
    
    [formatter setTimeZone:timeZone];

    NSDate* date = [formatter dateFromString:formatTime]; //------------将字符串按formatter转成nsdate
    
    //时间转时间戳的方法:
    
    NSInteger timeSp = [[NSNumber numberWithDouble:[date timeIntervalSince1970]] integerValue];
    
    NSLog(@"将某个时间转化成 时间戳&&&&&&&timeSp:%ld",(long)timeSp); //时间戳的值
    
    
    
    return timeSp;
    
}

+(NSString *)timestampSwitchTime:(NSInteger)timestamp andFormatter:(NSString *)format{
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    
    [formatter setDateFormat:format]; // （@"YYYY-MM-dd hh:mm:ss"）----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
    
    NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];
    
    [formatter setTimeZone:timeZone];
    
    NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:timestamp];
    
    NSLog(@"1296035591  = %@",confromTimesp);
    
    
    NSString *confromTimespStr = [formatter stringFromDate:confromTimesp];
    
    
    
    //NSLog(@"&&&&&&&confromTimespStr = : %@",confromTimespStr);

    
    return confromTimespStr;
    
}
// 字典转json字符串方法

+(NSString *)convertToJsonData:(NSDictionary *)dict

{
    
    NSError *error;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:&error];
    
    NSString *jsonString;
    
    if (!jsonData) {
        
        NSLog(@"%@",error);
        
    }else{
        
        jsonString = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding];
        
    }
    
    NSMutableString *mutStr = [NSMutableString stringWithString:jsonString];
    
    NSRange range = {0,jsonString.length};
    
    //去掉字符串中的空格
    
    [mutStr replaceOccurrencesOfString:@" " withString:@"" options:NSLiteralSearch range:range];
    
    NSRange range2 = {0,mutStr.length};
    
    //去掉字符串中的换行符
    
    [mutStr replaceOccurrencesOfString:@"\n" withString:@"" options:NSLiteralSearch range:range2];
    
    return mutStr;
    
}

/**
 数组转Json

 @param dict 数组
 @return 返回值
 */
+(NSString *)convertToJsonArray:(NSArray *)dict

{
    
    NSError *error;
    
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:&error];
    
    NSString *jsonString;
    
    if (!jsonData) {
        
        NSLog(@"%@",error);
        
    }else{
        
        jsonString = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding];
        
    }
    
    NSMutableString *mutStr = [NSMutableString stringWithString:jsonString];
    
    NSRange range = {0,jsonString.length};
    
    //去掉字符串中的空格
    
    [mutStr replaceOccurrencesOfString:@" " withString:@"" options:NSLiteralSearch range:range];
    
    NSRange range2 = {0,mutStr.length};
    
    //去掉字符串中的换行符
    
    [mutStr replaceOccurrencesOfString:@"\n" withString:@"" options:NSLiteralSearch range:range2];
    
    return mutStr;
    
}
/**
 根据证件类型返回文字
 
 @param type 证件类型
 @return 文字
 */
+(NSString *)backTitleAccordingIdCatogaryType:(NSInteger)type{
    if (type == 1) {
        return @"身份证";
    }else if (type == 2){
        return @"护照";
    }else if (type == 3){
        return @"军人证件";
    }else if (type == 4){
        return @"港澳通行证";
    }else if (type == 5){
        return @"台胞证";
    }else if (type == 6){
        return @"营业执照";
    }
    return @"其他";
}
/**
 根据类型转化为文字
 
 @param type 类型值
 @return 标题
 */
+(NSString *)backTitleAccordingSorceType:(NSInteger )type{
    if (type == 1) {
        return @"线下展业";
    }else if (type == 2){
        return @"网络媒体";
    }else if (type == 3){
        return @"转介绍";
    }
    return @"线上注册";
}
/**
 根据用户类型返回值
 
 @param type 用户类型
 @return 文字
 */
+(NSString *)backTitleAccordingCustormerCategoryType:(NSInteger)type{
    if (type == 1) {
        return @"储备客户";
    }else if (type == 2){
        return @"订单客户";
    }else if (type == 3){
        return @"APP注册客户";
    }
    return @"未知";
}
/**
 根据关系类型返回文字
 
 @param type 类型
 @return 文字
 */
+(NSString *)backTitleAccordingRelationType:(NSInteger)type{
    if (type == 1) {
        return @"家人";
    }else if (type == 2){
        return @"朋友";
    }else if (type == 3){
        return @"同事";
    }
    return @"其他";
}
/**
 根据审批类型返回所对应的值
 
 @param type 类型
 @return 对应文字
 */
+(NSString *)backTitleAccordingApprovalStatusType:(NSInteger)type{
    if (type == 0) {
        return @"未变更";
    }else if (type == -1){
        return @"变更未提交";
    }else if (type == 1){
        return @"变更待审批";
    }else if (type == 2){
        return @"变更审批通过";
    }else if (type == 3){
        return @"分总审批通过";
    }else if (type == -3){
        return @"分总审批未通过";
    }
    return @"变更审批未通过";
}

+(NSString *)backOrderStatusAccordingStatusType:(NSInteger)type{
//    未提交 = 0,
//    待审批 = 1,
//    客服审批通过 = 2,
//    客服审批未通过 = -2,
//    财务审批通过 = 3,
//    财务审批未通过 = -3,
//    撮合审批通过 = 4,
//    撮合审批未通过 = -4,
//    变更未提交 = -6,
//    变更待审批 = 6,
//    银行信息变更待审核 = 8,
//    变更审批未通过 = -7,
//    变更审批通过 = 7,
//    已作废 = -8,
//    已到期 = 9,
//    已提前赎回 = 10,
//    撮合通过后作废 = 11,
    if (type == 1) {
        return @"待审批";
    }else if (type == 2){
        return @"客服审批通过";
    }else if (type == 3){
        return @"财务审批通过";
    }else if (type == 4){
        return @"撮合审批通过";
    }else if (type == 5){
        return @"";
    }else if (type == 6){
        return @"变更待审批";
    }else if(type == 7){
        return @"变更审批通过";
    }else if(type == 8){
        return @"银行信息变更待审核";
    }else if(type == 9){
        return @"已到期";
    }else if(type == 10){
        return @"已提前赎回";
    }else if(type == 11){
        return @"撮合通过后作废";
    }else if(type == -2){
        return @"客服审批未通过";
    }else if(type == -3){
        return @"财务审批未通过";
    }else if(type == -4){
        return @"撮合审批未通过";
    }else if(type == -6){
        return @"变更未提交";
    }else if(type == -7){
        return @"变更审批未通过";
    }else if(type == -8){
        return @"已作废";
    }
    return @"未提交";
}
+(NSString *)backProductTypeAccordingStatusType:(NSInteger)type{
    if (type == 1) {
        return @"企来贷";
    }else if (type == 2){
        return @"易收益";
    }else if (type == 3){
        return @"基金";
    }else if (type == 4){
        return @"银丰收";
    }
    return @"交保";
}
@end
